package com.yssw.image;

import com.yssw.common.config.RuoYiConfig;
import com.yssw.common.core.domain.AjaxResult;
import com.yssw.common.utils.StringUtils;
import com.yssw.common.utils.file.FileUploadUtils;
import com.yssw.common.utils.file.FileUtils;
import com.yssw.framework.config.ServerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author KnockingFarmers
 * @Date 2023/4/27 10:06
 * @PackageName:com.yssw.image
 * @ClassName: ImagesController
 * @Description: TODO
 * @Version 1.0
 */
@RestController
@RequestMapping("/image")
public class ImagesController {
    @Autowired
    private ServerConfig serverConfig;


    /**
     * 通用上传请求（单个）
     */
    @PostMapping("/logo")
    public AjaxResult uploadFile(MultipartFile file) throws Exception {
        try {

            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            String url = serverConfig.getUrl() + fileName;
            AjaxResult ajax = AjaxResult.success();
            ajax.put("url", url);
            ajax.put("fileName", fileName);
            ajax.put("newFileName", FileUtils.getName(fileName));
            ajax.put("originalFilename", file.getOriginalFilename());
            return ajax;
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }
}
